package ui;

import java.awt.Dimension;

import control.SystemUIControl;
import sniff.pack.CommonPacket;
import ui.util.YPUIUtil;
import ui.widget.TitleRetractablePanel;
import ui.widget.TxtPanel;
import ui.widget.XPanel;
import ui.widget.YPanel;

public class DetailMsgPanel extends YPanel {
	private CommonPacket pack;
	private final String space = "     ";
	private final String[] TITLE = { "Frame Header", "Internet Protocol", "Transmission Protocol",
			"Application Protocol" };

	public DetailMsgPanel() {

	}

	private void addMsgPanel(String title, String msgs) {
		String[] strs = msgs.split("\n");
		TitleRetractablePanel t = new TitleRetractablePanel(this.getWidth(),30,title);
		for (String s : strs) {
			TxtPanel tx = new TxtPanel(this.getWidth(),30,space + s);
			t.getMainPanel().add(tx);
			tx.setMaximumSize(new Dimension(YPUIUtil.getScreenWidth(), 30));
		}
		add(t);
	}

	private void init() {
		removeAll();
		String[] values = pack.toString().split(SystemUIControl.SPLITSTRING + "\n");
		for (int i = 0; i < values.length; i++) {
			String value = values[i];
			addMsgPanel(TITLE[i], value);
		}
		validate();
		repaint();
		revalidate();
	}

	public CommonPacket getPack() {
		return pack;
	}

	public void setPack(CommonPacket pack) {
		this.pack = pack;
		init();
	}

}
